package com.test.controller;

import com.test.pojo.DinnerTable;
import com.test.service.DinnerTableService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class DinnerTableCtrl {
    @Resource
    private DinnerTableService dinnerTableService;

    /*
     *获取所有的餐桌
     */
    @RequestMapping("/getAllDinnerTable")
    public String getAllDinnerTable(Model model){
        List<DinnerTable> dinnerTableList = dinnerTableService.getAllDinnerTable();
        model.addAttribute("dinnerTableList",dinnerTableList);
        return "boardList";
    }


    /*int
     *跳转到餐桌更新页面
     */
//    @GetMapping(value = "/editFoodType")
//    public String searchFoodTypeByID(@RequestParam("foodTypeId") int id, Model model){
//
//        FoodType foodType = cuisineService.getFoodTypeByID(id);
//
//        if (foodType != null) {
//            model.addAttribute("foodType",foodType);
//            return "EditFoodType";
//        }else {
//            return "redirect:/getAllFoodType";
//        }
//    }

    /*
     * 更改餐桌名称
     * */
//    @PostMapping(value ="/doEditFoodType/{id}")
//    public String doEditFoodType(@PathVariable("id") int id, FoodType foodType, Model model){
//
//        int num = cuisineService.updateFoodType(foodType);
//        if(num > 0){
//            model.addAttribute("msg","菜系更新成功！");
//        }else{
//            model.addAttribute("msg","菜系更新失败！");
//        }
//        return "forward:/getAllFoodType";
//    }

    /*删除餐桌*/
    @RequestMapping(value ="/delDinnerTable/{id}",method = RequestMethod.GET)
    public String delDinnerTable(@PathVariable("id") int id, Model model){
        int num = dinnerTableService.deleteDinnerTable(id);
        System.out.println(num);
        if(num > 0){
            model.addAttribute("msg","餐桌删除成功！");
        }else if (num == -1){
            model.addAttribute("msg","该餐桌被预定，所以不能删除该餐桌");
        }else{
            model.addAttribute("msg","餐桌删除失败！");
        }
        return "forward:/getAllDinnerTable";
    }

    /*
     * 转到添加页面
     * */
    @RequestMapping(value = "/goToAddDinnerTable")
    public  String goToAddDinnerTable(){
        return "saveBoard";
    }

    /*
     * 添加餐桌
     **/
    @PostMapping(value ="/doAddDinnerTable")
    public String addFootType(DinnerTable dinnerTable,Model model){

        int num = dinnerTableService.addDinnerTable(dinnerTable);
        if(num > 0){
            model.addAttribute("msg","菜系【"+dinnerTable.getTableName()+"】添加成功！");
        }else{
            model.addAttribute("msg","菜系添加失败！");
        }
        return "forward:/getAllDinnerTable";
    }


//    /**
//     * 查询所有未预定的餐桌
//     */
//    @RequestMapping("/findNoUseTable")
//    public String findNoUseTable(Model model){
//        List<DinnerTable> dinnerTables = dinnerTableService.findNoUseTable();
//        model.addAttribute("dinnerTables",dinnerTables);
//        return "boardList";
//    }

}
